package com.example.server.web.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.server.web.domain.Academy;
import com.example.server.web.domain.Class;
import com.example.server.web.domain.Major;
import com.example.server.web.domain.page.PageQueryDto;
import com.example.server.web.domain.vo.ClassVo;
import com.example.server.web.exception.CustomException;
import com.example.server.web.mapper.ClassMapper;
import com.example.server.web.service.AcademyService;
import com.example.server.web.service.ClassService;
import com.example.server.web.service.MajorService;
import com.example.server.web.utils.MyObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 作者
 * @since 2023-04-04
 */
@Service
public class ClassServiceImpl extends ServiceImpl<ClassMapper, Class> implements ClassService {

    @Autowired
    private ClassMapper classMapper;

    @Autowired
    private AcademyService academyService;

    @Autowired
    private MajorService majorService;

    @Override
    public Map queryByPage(PageQueryDto dto) {
        LambdaQueryWrapper<Class> wrapper = new LambdaQueryWrapper<>();
        //模糊查询
        if(StringUtils.isNotBlank((String)dto.getParams("className"))) {
            wrapper.like(Class::getClassName, (String)dto.getParams("className"));
        }
        //根据年级查询
        if (StringUtils.isNotEmpty((String)dto.getParams("grade"))) {
            wrapper.eq(Class::getGrade,(String)dto.getParams("grade"));
        }
        //根据学院查询
        if (StringUtils.isNotEmpty((String)dto.getParams("academyId"))) {
            wrapper.eq(Class::getAcademyId,(String)dto.getParams("academyId"));
        }
        Page<Class> page = this.page(new Page<>(dto.getPage(), dto.getLimit()), wrapper);
        //查询对应的信息
        List<ClassVo> classVos = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(page.getRecords())){
            for (Class record : page.getRecords()) {
                ClassVo classVo = new ClassVo();
                BeanUtils.copyProperties(record,classVo);
                //查询学院名称
                Academy academy = academyService.getById(record.getAcademyId());
                if (MyObjectUtils.isNotNull(academy)){
                    classVo.setAcademyName(academy.getAcademyName());
                } else {
                    classVo.setAcademyName("该学院可能被删除");
                }
                classVo.setAcademyName(academy.getAcademyName());
                //查询专业名称
                Major major = majorService.getById(record.getMajorId());
                if (MyObjectUtils.isNotNull(major)){
                    classVo.setMajorName(major.getName());
                } else {
                    classVo.setMajorName("该专业可能被删除");
                }
                classVos.add(classVo);
            }
        }
        Map<String, Object> map = new HashMap<>();
        map.put("classList",classVos);
        map.put("total",page.getTotal());
        return map;
    }

    @Override
    public Integer add(Class c) {
        LambdaQueryWrapper<Class> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Class::getClassName,c.getClassName());
        Class one = this.getOne(wrapper);
        //判断数据库中是否存在重复的班级名称
        if(one!=null){
            throw  new CustomException("已存在该班级名称，请换一个！");
        }
        return classMapper.insert(c);
    }

    @Override
    public Integer updateStudentCount(Long classId,Integer count) {
        Class c = classMapper.selectById(classId);
        return classMapper.updateStudentCount(classId,c.getClassPersonNum()+count);
    }

    @Override
    public Integer modify(Class c) {
        LambdaQueryWrapper<Class> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Class::getClassName,c.getClassName());
        Class one = this.getOne(wrapper);
        //判断数据库中是否存在重复的班级名称
        if(one!=null&&one.getId()!=c.getId()){
            throw  new CustomException("已存在该班级名称，请换一个！");
        }
        boolean b = this.updateById(c);
        return b==true?1:0;
    }

    @Override
    public Integer deletes(List<Integer> ids) {
        boolean b = this.removeByIds(ids);
        return b==true?1:0;
    }

    @Override
    public List<Class> queryByAcademyId(Long academyId) {
        LambdaQueryWrapper<Class> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Class::getAcademyId,academyId);
        List<Class> classList = classMapper.selectList(wrapper);
        return classList;
    }
}
